<?php
$nav_page = 2;
include_once 'common/personal_top.php';
include_once '../Class/DB.php';
$db=new DB();
$orders=$db->get_uid_order($_SESSION["User"]["uid"]);
$page=$_GET["page"];
if(!isset($page))
{
	$page=1;
}
$numPerPage=4;
if(isset($_POST["id"]))
{
	$orders=array();
	$orders[0]=$db->get_oid_order($_POST["id"]);
	
}
$totalPage=ceil(count($orders)/$numPerPage);
if($page>$totalPage)
{
	$page--;
}
$startCount=($page-1)*$numPerPage; 
$orders=$db->uid_order_paging($orders,$startCount,$numPerPage);
?>

<div id="book" class="block_mode">
	<div class="block_context">
		<form action="personal_book.php" method="post">
			<div id="search">
				<h3>Order Query</h3>
				<div class="block_context">
					<p>Order Id：</p>
					<input type="text" name="id"/>
					<input type="submit" value="Query"/>
				</div>
			</div>
		</form>
		
		<table border="0">
			<tr>
				<th>
					<p>Order Id</p>
				</th>
				<th>
					<p>Order Content</p>
				</th>
				<th>
					<p>Order Time</p>
				</th>
				<th>
					<p>Should Pay($)</p>
				</th>
				<th>
					<p>Order State</p>
				</th>
				<th>
					<p>Operation</p>
				</th>
			</tr>
			<?php 
			$i=0;
			while(isset($orders[$i]))
			{
			?>
			<tr>
				<td>
					<p><?php echo $orders[$i]["oid"];?></p>
				</td>
				<td>
					<p><?php echo $orders[$i]["p_name"];?></p>
				</td>
				<td>
					<p><?php echo $orders[$i]["order_date"];?></p>
				</td>
				<td>
					<p><?php echo $orders[$i]["should_pay"];?></p>
				</td>
				<td>
					<p><?php if($orders[$i]["state"]==1)echo "Not Paid";else if($orders[$i]["state"]==2)echo "Paid";else if($orders[$i]["state"]==3)echo "Delivered";else if($orders[$i]["state"]==4)echo "Received";?></p>
				</td>
				<td>
				<?php if($orders[$i]["state"]==1){?>
					<a href="cancelOrder.php?id=<?php echo $orders[$i]["oid"];?>">Cancel</a>
				<?php }?>
				</td>
			</tr>
			<?php 
			$i++;
			}
			?>
		</table>
		<?php 
		if($i==0)
			{?>
			<br>
			<div align="center"><p>Order list is empty! or Cannot find specific order!</p></div>
			<?php 	
			}else{
			?>
		<div id="page">
			<?php if ($page != 1) { ?>
			<a href="personal_book.php?page=<?php echo $page - 1;?>">Back</a> 
			<?php }
			for ($i=1;$i<=$totalPage;$i++) {  
				if($i==$page){
					?>
					<p><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="personal_book.php?page=<?php echo $i;?>"><?php echo $i ;?></a>
			<?php
			}
			if ($page<$totalPage) { 
			?>
				<a href="personal_book.php?page=<?php echo $page + 1;?>">Next</a>
			<?php
					} 
			?>
		</div>
		<?php }?>
	</div>
</div>
<?php
include_once 'common/personal_tail.php';
?>